Texture2D diffuse;

cbuffer Screen
{
	matrix	mProj;
};

cbuffer Model
{
	matrix	mWorld;
};

cbuffer Camera
{
	matrix	mView;
};

cbuffer Lighting
{
	float4	mLightColor;
}

cbuffer Shading
{
	float4	mUvTransformation;
	float4	mFrameSize;
	float	mSaturation;
	float	mOpacity;
};

SamplerState Linear;

struct VsInput
{
	float2	mPos : POSITION;
	float2	mUv : UV;
};

struct PsInput
{
	float4	mPos : SV_POSITION;
	float2	mUv : TEXCOORD0;
};

struct PsOutput
{
	float4	mColor : SV_Target;
};

PsInput VS(VsInput input)
{
	PsInput output = (PsInput)0;
	output.mPos = float4(input.mPos, 0.5f, 1.0f);
	output.mPos = mul(output.mPos, mWorld);
	output.mPos = mul(output.mPos, mView);
	output.mPos = mul(output.mPos, mProj);
	output.mUv.x = input.mUv.x;
	output.mUv.y = input.mUv.y;
	return output;
}

PsOutput PS_SpotLight(PsInput input)
{
	PsOutput output = (PsOutput)0;
	float factor = 1.0f - length(input.mUv.xy);
	if(factor < 0.0f)
		factor = 0.0f;
	if(factor > 1.0f)
		factor = 1.0f;
	output.mColor = mLightColor;
	output.mColor.a *= factor;
	return output;
}

PsOutput PS_Sprite(PsInput input)
{
	PsOutput output = (PsOutput)0;
	//output.mColor = float4(1.0f, 0.0f, 0.0f, 1.0f);
	//return output;
	float2 uv;
	uv.x = input.mUv.x;
	uv.y = input.mUv.y;
	uv.x = uv.x * mUvTransformation.z;
	uv.y = uv.y * mUvTransformation.w;
	uv.x *= (mFrameSize.x - mFrameSize.z);
	uv.y *= (mFrameSize.y - mFrameSize.w);
	uv.x = fmod(uv.x, mFrameSize.x);
	uv.y = fmod(uv.y, mFrameSize.y);
	uv.x += mUvTransformation.x;
	uv.y += mUvTransformation.y;
	output.mColor = diffuse.Sample(Linear, uv);
	float avg = (output.mColor.r + output.mColor.g + output.mColor.b) / 3.0f;
	float4 desaturatedColor = float4(avg, avg, avg, output.mColor.a);
	output.mColor = mSaturation * output.mColor + (1.0f - mSaturation) * desaturatedColor;
	output.mColor.a *= mOpacity;
	return output;
}
